package com.example.modelsystem.controller;

import cn.hutool.json.JSONUtil;
import com.example.modelsystem.dao.RecentChatMapper;
import com.example.modelsystem.entity.RecentChatEntity;
import com.google.gson.Gson;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "聊天列表")
@RestController
public class ChatController {

    Gson gson = new Gson();

    @Autowired
    RecentChatMapper recentChatMapper;

    @ApiOperation(value = "获取最近聊天列表", notes = "返回列表内容")
    @ApiImplicitParam(paramType = "path", required = true, name = "userId", value = "用户Id", defaultValue = "1")
    @ApiResponses({
            @ApiResponse(code = 422, message = "用户Id不能为空"),
            @ApiResponse(code = 200, message = "获取成功", response = RecentChatEntity.class)
    })
    @GetMapping("/recent-chat-list/{userId}")
    public ResponseEntity getRecentChatList(@PathVariable Integer userId) {
        if (userId == null) {
            return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body("用户Id不能为空");
        }
        List<RecentChatEntity> data = recentChatMapper.selectByUserId(userId);
        return ResponseEntity.status(HttpStatus.OK).body(JSONUtil.toJsonStr(gson.toJson(data)));
    }
}
